package coreapi;

import javax.naming.Context;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 演示 ServletConfig 对象的使用
 */
public class ServletConfigUsage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ServletConfig对象封装了Servlet在web.xml中的配置信息，它对应一段<servlet>元素
        ServletConfig config = getServletConfig();

        //获得 <servlet-name>
        String servletName = config.getServletName();
        System.out.println("servlet Name = " + servletName);

        //获得 初始化参数
        Enumeration<String> params = config.getInitParameterNames();
        while (params.hasMoreElements()) {
            String param = params.nextElement();
            System.out.println(param + " = " + config.getInitParameter(param));
        }

        //获得 ServletContext
        ServletContext context = config.getServletContext();
    }
}